const {getStream} = require("./rtc/common");
const {ipcRenderer} = window.require('electron')

const localPc = new RTCPeerConnection()
const remotePc = new RTCPeerConnection()
let localVideo = window.document.getElementById('localVideo')
let remoteVideo = window.document.getElementById('remoteVideo')
//是userId呼叫peerId

ipcRenderer.on("dataAvailable", function (e, msgContainer) {
    let typ = msgContainer.type;
    switch (typ) {
        case 'start': {
            console.log('isonline', msgContainer)
            init(msgContainer)
            break
        }
        case "offer": {
            console.log(msgContainer.offer)
            onOffer(msgContainer);
            break;
        }
        case "answer": {
            onAnswer(msgContainer.answer);
            break;
        }
        case "candidate": {
            onCandidate(msgContainer);
            break;
        }
    }
})

init = function (msgContainer) {
    console.log('init', msgContainer)
    getStream().then(function (stream) {
        localPc.addStream(stream);
        localPc.createOffer().then(function (offer) {
            localPc.setLocalDescription(offer);
            send({
                type: "offer",
                offer: offer,
                from: msgContainer.userId,
                to: msgContainer.peerId
            });
        });

    });
    localPc.onicecandidate = function (e) {
        console.log("onicecandidate local");
        if (e.candidate) {
            send({
                type: "candidate",
                candidate: e.candidate,
                from: msgContainer.userId,
                to: msgContainer.peerId,
            });
        }
    };
}

send = function (data) {
    ipcRenderer.invoke('handleSendMsg', JSON.stringify(data))
}
onOffer = function (msgContainer) {
    console.log("offer", msgContainer.offer);
    getStream().then(function (stream) {
        remotePc.addStream(stream)
        remotePc.setRemoteDescription(msgContainer.offer);
        remotePc.createAnswer().then(function (answer) {
            remotePc.setLocalDescription(answer);
            send({
                type: "answer",
                answer: answer,
                from: msgContainer.to,
                to: msgContainer.from,
            });
        });

        remotePc.onicecandidate = function (e) {
            console.log("onicecandidate remote");
            if (e.candidate) {
                send({
                    type: "candidate",
                    candidate: e.candidate,
                    from: msgContainer.to,
                    to: msgContainer.from
                });
            }
        };
    })
}
onAnswer = function (answer) {
    console.log("answer", answer);
    localPc.setRemoteDescription(answer);
}

onCandidate = function (msgContainer) {
    localPc.addIceCandidate(new RTCIceCandidate(msgContainer.candidate))
    remotePc.addIceCandidate(new RTCIceCandidate(msgContainer.candidate))
}

remotePc.onaddstream = function (e) {
    remoteVideo.srcObject = e.stream
};

localPc.onaddstream = function (e) {
    localVideo.srcObject = e.stream
};
